import {Component, forwardRef, Input, OnInit} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';

@Component({
    selector: 'app-bootstrap4-input-save-text',
    templateUrl: './bootstrap4-input-save-text.component.html',
    styleUrls: ['./bootstrap4-input-save-text.component.css'],
    providers: [
        {
            provide: NG_VALUE_ACCESSOR,
            useExisting: forwardRef(() => Bootstrap4InputSaveTextComponent),
            multi: true
        }
    ]
})
export class Bootstrap4InputSaveTextComponent implements OnInit, ControlValueAccessor {

    @Input('title')
    title = 'title';

    fieldVal = 'fieldVal';

    @Input('errMsg')
    errMsg = null;

    @Input('readOnly')
    readOnly = false;

    private onChangeCallback: (_: any) => void = function () {
    };

    constructor() {
    }

    get value(): any {
        return this.fieldVal;
    }

    set value(v: any) {
        if (v !== this.fieldVal) {
            this.fieldVal = v;
            this.onChangeCallback(v);
        }
    }

    ngOnInit() {
    }

    writeValue(value: any): void {
        // throw new Error("Method not implemented.");
        if (value !== this.fieldVal) {
            this.fieldVal = value;
        }
    }

    registerOnChange(fn: any): void {
        // throw new Error("Method not implemented.");
        this.onChangeCallback = fn;
    }

    registerOnTouched(fn: any): void {
        // throw new Error("Method not implemented.");
        this.onChangeCallback = fn;
    }

    setDisabledState?(isDisabled: boolean): void {
        // throw new Error("Method not implemented.");
    }

}
